<!-- 以下代码为代码生成器生成，生成时间: 2018-03-18 23:08:38，作者：billjiang/475572229@qq.com-->
<section class="content-header">
    <h1>
        产品管理
        <small>列表</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="${basePath}"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">生产管理</a></li>
        <li class="active">产品管理</li>
    </ol>
</section>

<section class="content">
    <div class="row">
        <div class="col-md-7">
            <div class="box">
                <!-- /.box-header -->
                <div class="dataTables_filter" id="searchDiv" style="display: none">
                    <!--查询条件-->
                    <input type="search" placeholder="请输入产品名称" title="产品名称" name="name"
                                           class="form-control" id="name" operator="like" likeOption="true">
                    <select name="proType" id="proType" title="产品类别" data-flag="dictSelector" data-code="PROTYPE" data-placeholder="请选择产品类别" class="form-control select2" style="width: 120px;" data-blank="true"></select>
                    <div class="btn-group">
                        <button type="button" class="btn btn-primary" data-btn-type="search">查询</button>
                        <button type="button" class="btn btn-default" data-btn-type="reset">重置</button>
                    </div>
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" data-btn-type="add">新增</button>
                        <button type="button" class="btn btn-default" data-btn-type="edit">编辑</button>
                        <button type="button" class="btn btn-default" data-btn-type="delete">删除</button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="produce_table" class="table table-bordered table-striped table-hover">
                    </table>
                </div>
            </div>
        </div>
        
        <div class="col-md-5">
            <div class="box">
                <!-- /.box-header -->
                <div class="dataTables_filter" id="searchBatchDiv">
                	<input type="hidden"  name="proId" value="-1" id="proId"/>
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" data-btn-type="add_batch">新增</button>
                        <button type="button" class="btn btn-default" data-btn-type="edit_batch">编辑</button>
                        <button type="button" class="btn btn-default" data-btn-type="delete_batch">删除</button>
                        <button type="button" class="btn btn-default" data-btn-type="bind_flow">绑定生产流程</button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="batch_table" class="table table-bordered table-striped table-hover">
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    var form = $("#searchDiv").form({baseEntity: false});
    var form_batch = $("#searchBatchDiv").form({baseEntity: false});
    form.initComponent();
    form_batch.initComponent();
    var produceTable, winId = "produceWin";
    var batchTable, batch_winId = "batchWin";
    $(function (){
    	var produce_config = {
				//定义选中行事件
				rowClick : function(row, isSelected) {
					$("#proId").val(isSelected ? row.id : "-1");
					batchTable.reloadData();
				},
				//数据加载后，默认选中第一行
				loadComplete : function() {
					produceTable.selectFirstRow(true);
				}
			}
        produceTable = new CommonTable("produce_table", "produce", "searchDiv", produce_config);
        batchTable = new CommonTable("batch_table", "batch", "searchBatchDiv", null);
        //绑定按钮事件
        $("button[data-btn-type]").click(function () {
            var action = $(this).data("btn-type");
            var rowId = produceTable.getSelectedRowId();
            var batch_rowId = batchTable.getSelectedRowId();
            switch (action) {
                case "add":
                    modals.openWin({
                        winId: winId,
                        title: '新增产品',
                        width: "900px",
                        url: basePath + "/produce/edit"
                    })
                    break;
                case "edit":
                    if (!rowId) {
                        modals.info('请选择要编辑的行');
                        return false;
                    }
                    modals.openWin({
                        winId: winId,
                        title: '编辑产品【' + produceTable.getSelectedRowData().name + '】',
                        width: '900px',
                        url: basePath + "/produce/edit?id=" + rowId
                    });
                    break;
                case 'delete':
                    if (!rowId) {
                        modals.info('请选择要删除的行');
                        return false;
                    }
                    modals.confirm("是否要删除该行数据？", function () {
                        ajaxPost(basePath + "/produce/delete/" + rowId, null, function (data) {
                            if (data.success) {
                                produceTable.reloadRowData();
                            } else {
                                modals.info("产品数据被引用，不可删除！");
                            }
                        });
                    })
                    break;
                    
                case "add_batch":
                	if (!rowId) {
                         modals.info('请选择产品');
                         return false;
                    }
                    modals.openWin({
                        winId: batch_winId,
                        title: '新增批次',
                        width: "900px",
                        url: basePath + "/batch/edit?produce="+rowId
                    })
                    break;
                case "edit_batch":
                    if (!batch_rowId) {
                        modals.info('请选择要编辑的行');
                        return false;
                    }
                    modals.openWin({
                        winId: batch_winId,
                        title: '编辑批次',
                        width: '900px',
                        url: basePath + "/batch/edit?id=" + batch_rowId
                    });
                    break;
                    
                case "bind_flow":
                    if (!batch_rowId) {
                        modals.info('请选择要绑定的行');
                        return false;
                    }
                    modals.openWin({
                        winId: batch_winId,
                        title: '编辑批次',
                        width: '900px',
                        url: basePath + "/batch/bindFlow?id=" + batch_rowId
                    });
                    break;
                    
                case 'delete_batch':
                    if (!batch_rowId) {
                        modals.info('请选择要删除的行');
                        return false;
                    }
                    modals.confirm("是否要删除该行数据？", function () {
                        ajaxPost(basePath + "/batch/delete/" + batch_rowId, null, function (data) {
                            if (data.success) {
                                batchTable.reloadRowData();
                            } else {
                                modals.info("批次数据被引用，不可删除！");
                            }
                        });
                    })
                    break;
            }
        })
    })
    
    function renderDictType(value) {
		if (value) {
			var ret = value;
			ajaxPost(basePath + "/dict/get/" + value, null, function(data) {
				ret = data.name;
			})
			return ret;
		} else
			return value;
	}
    
    function rendFlow(value){
    	if(value){
    		var ret = value;
    		ajaxPost(basePath + "/pflow/get/" + value, null, function(data){
    			ret = data.name;
    		})
    		return ret;
    	}else{
    		return value;
    	}
    }
    
    function createQrcode(value){
    	var qrcodeTag = "<a href='javascript:void(0)' onclick='showQrcode(\"" + value + "\")'>生成二维码</a>";
    	return qrcodeTag;
    }
    
    function showQrcode(batchId){
    	modals.openWin({
            winId:'showQrcode',
            title:'生成二维码',
            width:'600px',
            height:'400px',
            url:basePath+"/batch/showQrcode?batchId="+batchId
         });
    }
    
</script>